<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="page.param.name" placeholder="关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="deleteData">
        删除
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="50" />
      <el-table-column align="center" label="学生姓名" prop="studentName"/>
      <el-table-column align="center" label="出发地" prop="startAddress"/>
      <el-table-column align="center" label="目的地" width="200" prop="endAddress"/>
      <el-table-column align="center" label="时间" width="100" prop="startTime"/>
      <el-table-column align="center" label="交通方式" width="100" prop="transport"/>
      <el-table-column align="center" label="车牌号" width="100" prop="carNum"/>
      <el-table-column align="center" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-show="page.total>0" :total="page.total" :current-page="page.page" :page-size="page.limit" @current-change="fetchData" />
  </div>
</template>

<script>
import { page, deleteByIds } from '@/api/city'
import { Message } from 'element-ui'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      loading: false,
      page: {
        page: 1,
        size: 10,
        total: 0,
        param: { name: '' }
      },
      multipleSelection: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取列表数据
    fetchData(val) {
      if (val != null && val !== '') {
        this.page.page = val
      }
      this.listLoading = true
      page(this.page).then(response => {
        this.list = response.result.records
        this.page.total = response.result.total
        this.listLoading = false
      })
    },
    // 选中改变
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 列表筛选
    handleFilter() {
      this.fetchData()
    },
    // 删除
    deleteData() {
      this.loading = true
      const ids = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach(item => {
          ids.push(item.id)
        })
        deleteByIds(ids).then(res => {
          this.loading = false
          if (res.code ==='200'){
            this.fetchData()
            Message.success(res.result)
          }
        }).catch((err) => {
          this.loading = false
          console.log(err)
        })
      } else {
        this.loading = false
        Message.error('请要删除的数据！')
      }
    }
  }
}
</script>
